package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.qf.entity.Student;
import com.qf.entity.StudentVO;
import com.qf.service.StudentService;
import com.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@CrossOrigin
public class StudentController {
    @Autowired
    private StudentService studentService;
    //登录
    @PostMapping("/login")
    public R login(@RequestBody Student student){
        Student stu=studentService.login(student.getSname());
        if(stu==null){
            return R.fail();
        }
        return R.success(stu);
    }

    //查询一个数据
    @GetMapping("/stu/find")
    public R findById(int id){
        Student stu=studentService.findById(id);
        return R.success(stu);

    }
    //查询全部数据
    @GetMapping("/stu/list")
    public R findAll(){
        List<Student> list=studentService.findAll();
        return R.success(list);
    }
    //分页查询全部
    @GetMapping("/stu/list/page")
    public R findAll(int pageSize,int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list=studentService.findAll();
        return R.success(list);
    }
    //条件查询，根据年龄或姓名模糊查询
    @GetMapping("/stu/search")
    public R findByKeyWord(@RequestParam HashMap<String,Object> map){
        List<Student> list=studentService.findByKeyWord(map);
        return R.success(list);
    }
    //添加学生信息
    @GetMapping("/stu/add")
    public R add(Student student){
        studentService.add(student);
        return R.success();
    }
    //更新（全表更新）
    @PostMapping("/stu/edit")
    public R edit(Student student){
        studentService.edit(student);
        return R.success();
    }
    //删除
    @GetMapping("/stu/del")
    public R deleteById(int id){
        studentService.deleteById(id);
        return R.success();
    }
    //批量删除
    @GetMapping("/stu/del/ids")
    public R deleteByIds(@RequestParam List<Integer> ids){
        studentService.deleteByIds(ids);
        return R.success();
    }
    //查询所有学生信息以及关联班级信息
    @GetMapping("/stu/class")
    public R findAllStudentAndClassroom(){
        List<StudentVO> list=studentService.findAllStudentAndClassroom();
        return R.success(list);
    }
    //根据id查询1个学生信息以及关联的所有学科信息
    @GetMapping("/stu/subjects")
    public R findStudentAndAllSubjectByStuid(int id){
        StudentVO studentVO=studentService.findStudentAndAllSubjectByStuid(id);
        return R.success(studentVO);
    }
    //根据id查询1个学生信息以及关联的所有学科信息以及每个学科关联的老师信息
    @GetMapping("/stu/teacher")
    public R findStudentAndAllSubjectAndTeacherByStuid(int id){
        StudentVO studentVO=studentService.findStudentAndAllSubjectAndTeacherByStuid(id);
        return R.success(studentVO);
    }
    //查询学生信息以及关联的教室信息，单表查，先查学生表根据cid查教室表
    @GetMapping("/stu/class/v2")
    public R findAllStudentAndClassroomV2(){
        List<StudentVO> list=studentService.findAllStudentAndClassroomV2();
        return R.success(list);
    }
    //查询一个学生信息以及所学习的所有学科信息,单表查
    @GetMapping("/stu/subjects/v2")
    public R findStudentAndAllSubjectByStuidV2(int id){
        StudentVO studentVO=studentService.findStudentAndAllSubjectByStuidV2(id);
        return R.success(studentVO);
    }


}
